3 struct MapVertices: View {
8 let padding = CGFloat(5.0)
11 ZStack(alignment: .topLeading) {
12 ForEach(vertices, id: \.id) { vertex in
13 getVertexShape(vertex).fill(Color.map.vertexColor)
14 Text(vertex.label.replacingOccurrences(of: "\\n", with: "\n")).font(.theme.vertexLabel)
15 .foregroundColor(.map.labelColor)
16 .shadow(color: .white, radius: 0, x: -0.5, y: -0.5)
17 .shadow(color: .white, radius: 0, x: 0.5, y: 0.5)
20 width: w(vertex.position.x) + vertexSize.width + padding,
21 height: h(vertex.position.y) + 7.0))
26 func h(_ dimension: CGFloat) -> CGFloat {
27 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
30 func w(_ dimension: CGFloat) -> CGFloat {
31 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
34 func getVertexShape(_ vertex: Vertex) -> Path {
40 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
47 x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
48 height: vertexSize.height
53 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
56 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
59 to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
61 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
66 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
69 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
72 path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
74 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
76 }.strokedPath(StrokeStyle(lineWidth: 2.0, lineCap: .butt))
81 struct MapVertices_Previews: PreviewProvider {
82 static var previews: some View {
84 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
86 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
87 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
88 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
89 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),